package org.stars.daostars.core.runtime;

import org.stars.daostars.core.DaoMap;
import org.stars.daostars.events.DaoEventHandler;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.ResultSetHandler;


/**
 * 
 * @author Francesco Benincasa (908099)
 * @author Alex Svetina (908303)
 * @version 1.0.0.23-ago-200614.33.46
 * @param <T> 
 */
public class EntityBeanRsHandler<T> implements ResultSetHandler<List<T>>, Serializable
{
	
	/**
	 * tempo di esecuzione della query
	 */
	private long	executionTimeOfSql;
	
	/**
	 * tempo di esecuzione per il popolamento della lista
	 */
	private long	executionTimeOfRetriveList;
	
	
    /**
	 * serial id
	 */
	private static final long serialVersionUID = 2420058849201244636L;

	/**
     * The Class of beans produced by this handler.
     */
    private Class<T> type;
    
    /**
     * dao name
     */
    private String daoName;

    /**
     * The RowProcessor implementation to use when converting rows
     * into beans.
     */
	private EntityBeanRowProcessor convert;

    private DaoEventHandler onGetEntityEventHandler;
    
    /**
     * mappa di conversione. Puo' anche essere non definita
     */
    protected DaoMap conversionMap;
    
    /**
     * Creates a new instance of BeanListHandler.
     *
     * @param type The Class that objects returned from <code>handle()</code>
     * are created from.
     */
	public EntityBeanRsHandler(Class<T> type) {
        this(type,new EntityBeanRowProcessor());
    }
    
    /**
     * Recupera l'elenco delle colonne recuperate dall'ultimo result set.
     * Ovviamente se non esiste alcun result set invocato, questo metodo
     * restituisce <code>null</code>.
     * 
     * @return
     * 		array dei nomi delle colonne
     */
    public String[] getColumns()
    {
    	return convert.getColumns();
    }

    /**
     * Creates a new instance of BeanListHandler.
     *
     * @param type The Class that objects returned from <code>handle()</code>
     * are created from.
     * @param convert The <code>RowProcessor</code> implementation
     * to use when converting rows into beans.
     */
	public EntityBeanRsHandler(Class<T> type, EntityBeanRowProcessor convert) {
        this.type = type;
        this.convert = convert;
        
        this.onGetEntityEventHandler=null;
        
        this.conversionMap=null;
    }

    /**
     * Convert the <code>ResultSet</code> rows into a <code>List</code> of
     * beans with the <code>Class</code> given in the constructor.
     *
     * @return A <code>List</code> of beans (one for each row), never
     * <code>null</code>.
     *
     * @throws SQLException
     * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
     */
    @Override
	public List<T> handle(ResultSet rs) throws SQLException {
    	convert.setDaoEventHandler(onGetEntityEventHandler);
    	convert.setDaoName(daoName);
    	convert.setEntityName(type.getName());
    	convert.setConversionMap(conversionMap);
    	
        return convert.toBeanList(rs, type);
    }

	/**
	 * Getter dell'attributo onGetEntityEventHandler.
	 * @return
	 *		the onGetEntityEventHandler
	 */
	public DaoEventHandler getOnGetEntityEventHandler() {
		return onGetEntityEventHandler;
	}

	/**
	 * Setter dell'attributo onGetEntityEventHandler.
	 * @param onGetEntityEventHandler
	 *		the onGetEntityEventHandler to set
	 */
	public void setOnGetEntityEventHandler(
			DaoEventHandler onGetEntityEventHandler) {
		this.onGetEntityEventHandler = onGetEntityEventHandler;
	}

	/**
	 * @return the executionTimeOfRetriveList
	 */
	public long getExecutionTimeOfRetriveList() {
		return executionTimeOfRetriveList;
	}

	/**
	 * @param executionTimeOfRetriveList the executionTimeOfRetriveList to set
	 */
	public void setExecutionTimeOfRetriveList(long executionTimeOfRetriveList) {
		this.executionTimeOfRetriveList = executionTimeOfRetriveList;
	}

	/**
	 * @return the executionTimeOfSql
	 */
	public long getExecutionTimeOfSql() {
		return executionTimeOfSql;
	}
	
	/**
	 * @return the total execution time
	 */
	public long getTotalExecutionTime() {
		return executionTimeOfSql+executionTimeOfRetriveList;
	}


	/**
	 * @param executionTimeOfSql the executionTimeOfSql to set
	 */
	public void setExecutionTimeOfSql(long executionTimeOfSql) {
		this.executionTimeOfSql = executionTimeOfSql;
	}

	/**
	 * Getter dell'attributo daoName
	 * @return the daoName
	 */
	public String getDaoName() {
		return daoName;
	}

	/**
	 * Setter dell'attributo daoName
	 * @param daoName the daoName to set
	 */
	public void setDaoName(String daoName) {
		this.daoName = daoName;
	}

	public void setConversionMap(DaoMap value) {
		conversionMap=value;
		
	}
}
